package com.hua.yuan.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.hua.yuan.R;
import com.hua.yuan.base.Constents;
import com.hua.yuan.ui.VideoDisActivity;
import com.hua.yuan.ui.data.VideoDataRequest;
import com.hua.yuan.ui.module.CommentInfoBean;
import com.hua.yuan.ui.module.MovieInfo69Bean;
import com.hua.yuan.util.GlideCircleTransform;
import com.hua.yuan.util.ToastUtil;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.DecimalFormat;
import java.util.List;

import cm.hua.yuan.common.utils.PreferenceUtils;
import okhttp3.Call;

/**
 * Created by huangyang on 2018/5/10.
 * 视频描述界面
 */

public class VideoDesAdapter extends BaseAdapter {

    private Context context;
    private List<CommentInfoBean> list;
    private List<MovieInfo69Bean> movieList;


    public void setMovieList(List<MovieInfo69Bean> movieList) {
        this.movieList = movieList;
    }

    private DecimalFormat df = new DecimalFormat("######0.0");


    public VideoDesAdapter(Context context) {
        this.context = context;
    }

    public List<CommentInfoBean> getList() {
        return list;
    }

    public void setList(List<CommentInfoBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int i) {

        return list == null ? null : list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return list == null ? 0 : i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {


        if (getItemViewType(i) == 0) {

            ViewHolderLike viewHolderLike;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.video_dis_like_view, null);
                viewHolderLike = new ViewHolderLike(view);
                view.setTag(viewHolderLike);
            } else {
                viewHolderLike = (ViewHolderLike) view.getTag();
            }

            viewHolderLike.setData(movieList);

            return view;

        } else {

            ViewHolder viewHolder;

            if (view == null) {
                view = LayoutInflater.from(context).inflate(R.layout.video_dis_comment_view, null);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }

            viewHolder.bind(i);

            return view;

        }


    }


    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public int getItemViewType(int position) {

        CommentInfoBean bean = list.get(position);

        if (bean.getType() == 0) {
            return 0;
        } else {
            return 1;
        }

    }

    class ViewHolder {

        private ImageView videoDisCommentImg;
        private TextView videoDisCommentName;
        private TextView videoDisCommentMsg;
        private TextView videoDisCommentTime;
        private LinearLayout videoCommentDZLl;
        private ImageView videoDisCommentDZTip;
        private TextView videoDisCommentDZNum;

        ViewHolder(View view) {
            videoDisCommentImg = view.findViewById(R.id.videoDisCommentImg);

            videoDisCommentName = view.findViewById(R.id.videoDisCommentName);
            videoDisCommentMsg = view.findViewById(R.id.videoDisCommentMsg);
            videoDisCommentTime = view.findViewById(R.id.videoDisCommentTime);
            videoCommentDZLl = view.findViewById(R.id.videoCommentDZLl);
            videoDisCommentDZTip = view.findViewById(R.id.videoDisCommentDZTip);
            videoDisCommentDZNum = view.findViewById(R.id.videoDisCommentDZNum);


        }

        @SuppressLint("SetTextI18n")
        void bind(int i) {

            final CommentInfoBean bean = list.get(i);

            //显示评论人的图片


            if (TextUtils.isEmpty(bean.getUser_img())) {

//                String url = "http://a0.att.hudong.com/16/91/300001062059131423910670544_950.jpg";
                Glide.with(context)
                        .load(R.drawable.default_img)
                        .asBitmap()
                        .transform(new GlideCircleTransform(context))
                        .into(videoDisCommentImg);

            } else {
                Glide.with(context)
                        .load(bean.getUser_img())
                        .asBitmap()
                        .transform(new GlideCircleTransform(context))
                        .into(videoDisCommentImg);
            }


            //显示品论人的名字
            videoDisCommentName.setText(bean.getUser_nick());

            //显示评论内容
            videoDisCommentMsg.setText(bean.getContent());

            //点赞个数
            videoDisCommentDZNum.setText(String.valueOf(bean.getLikes()));

            //显示时间
            videoDisCommentTime.setText(bean.getTime());

            videoCommentDZLl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    ToastUtil.showShortToast("点赞了");
                    if ("0".equals(bean.getHas_like())) {

                        if (context instanceof VideoDisActivity) {
                            VideoDisActivity activity = (VideoDisActivity) context;

                            if (activity.isLogin()) {
                                likeFun(bean.getId(), bean);
                            }

                        }


                    } else {
                        ToastUtil.showShortToast( "您已点赞");
                    }


                }
            });
            //是否点赞
            if ("1".equals(bean.getHas_like())) {
                //点赞了
                videoDisCommentDZTip.setImageResource(R.mipmap.video_dis_comment);
            } else {
                //没点赞
                videoDisCommentDZTip.setImageResource(R.mipmap.video_dis_comment_1);
            }

        }

    }

    class ViewHolderLike {

        private LinearLayout movieLl1;
        private ImageView videoDisImage1;
        private TextView videoDisName1;
        private TextView videoDisTv1;
        private TextView videoDisPlayNum1;

        private LinearLayout movieLl2;
        private ImageView videoDisImage2;
        private TextView videoDisName2;
        private TextView videoDisTv2;
        private TextView videoDisPlayNum2;

        private LinearLayout movieLl3;
        private ImageView videoDisImage3;
        private TextView videoDisName3;
        private TextView videoDisTv3;
        private TextView videoDisPlayNum3;

        private LinearLayout disLikeLl;


        private LinearLayout disCommentLl;


        ViewHolderLike(View view) {
            movieLl1 = view.findViewById(R.id.movieLl1);
            videoDisImage1 = view.findViewById(R.id.videoDisImage1);
            videoDisName1 = view.findViewById(R.id.videoDisName1);
            videoDisTv1 = view.findViewById(R.id.videoDisTv1);
            videoDisPlayNum1 = view.findViewById(R.id.videoDisPlayNum1);

            movieLl2 = view.findViewById(R.id.movieLl2);
            videoDisImage2 = view.findViewById(R.id.videoDisImage2);
            videoDisName2 = view.findViewById(R.id.videoDisName2);
            videoDisTv2 = view.findViewById(R.id.videoDisTv2);
            videoDisPlayNum2 = view.findViewById(R.id.videoDisPlayNum2);

            movieLl3 = view.findViewById(R.id.movieLl3);
            videoDisImage3 = view.findViewById(R.id.videoDisImage3);
            videoDisName3 = view.findViewById(R.id.videoDisName3);
            videoDisTv3 = view.findViewById(R.id.videoDisTv3);
            videoDisPlayNum3 = view.findViewById(R.id.videoDisPlayNum3);

            disLikeLl = view.findViewById(R.id.disLikeLl);

            disCommentLl = view.findViewById(R.id.disCommentLl);

            movieLl1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (movieList != null && movieList.size() > 0) {
                        MovieInfo69Bean bean = movieList.get(0);
                        if (context instanceof VideoDisActivity) {
                            VideoDisActivity activity = (VideoDisActivity) context;
                            activity.startActivity(new Intent(activity, VideoDisActivity.class).putExtra("id", bean.getId()));
                            activity.finish();
                        }
                    }

                }
            });

            movieLl2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (movieList != null && movieList.size() > 1) {
                        MovieInfo69Bean bean = movieList.get(1);
                        if (context instanceof VideoDisActivity) {
                            VideoDisActivity activity = (VideoDisActivity) context;
                            activity.startActivity(new Intent(activity, VideoDisActivity.class).putExtra("id", bean.getId()));
                            activity.finish();
                        }
                    }

                }
            });


            movieLl3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if (movieList != null && movieList.size() > 2) {
                        MovieInfo69Bean bean = movieList.get(2);
                        if (context instanceof VideoDisActivity) {
                            VideoDisActivity activity = (VideoDisActivity) context;
                            activity.startActivity(new Intent(activity, VideoDisActivity.class).putExtra("id", bean.getId()));
                            activity.finish();
                        }
                    }

                }
            });

        }


        void setData(List<MovieInfo69Bean> movieList) {

            if (list == null) {
                disCommentLl.setVisibility(View.GONE);

            } else {
                if (list.size() == 1) {
                    disCommentLl.setVisibility(View.GONE);
                }
            }

            if (movieList == null) {
                movieLl1.setVisibility(View.GONE);
                movieLl2.setVisibility(View.GONE);
                movieLl3.setVisibility(View.GONE);
                disLikeLl.setVisibility(View.GONE);
            } else if (movieList.size() == 1) {

                movieLl1.setVisibility(View.VISIBLE);
                movieLl2.setVisibility(View.GONE);
                movieLl3.setVisibility(View.GONE);
                disLikeLl.setVisibility(View.VISIBLE);

                showItemUi(videoDisImage1, videoDisName1, videoDisTv1, videoDisPlayNum1, movieList.get(0));

            } else if (movieList.size() == 2) {

                movieLl1.setVisibility(View.VISIBLE);
                movieLl2.setVisibility(View.VISIBLE);
                movieLl3.setVisibility(View.GONE);
                disLikeLl.setVisibility(View.VISIBLE);

                showItemUi(videoDisImage1, videoDisName1, videoDisTv1, videoDisPlayNum1, movieList.get(0));
                showItemUi(videoDisImage2, videoDisName2, videoDisTv2, videoDisPlayNum2, movieList.get(1));


            } else {

                if (movieList.size() == 0) {

                    movieLl1.setVisibility(View.GONE);
                    movieLl2.setVisibility(View.GONE);
                    movieLl3.setVisibility(View.GONE);
                    disLikeLl.setVisibility(View.GONE);

                } else {
                    movieLl1.setVisibility(View.VISIBLE);
                    movieLl2.setVisibility(View.VISIBLE);
                    movieLl3.setVisibility(View.VISIBLE);

                    disLikeLl.setVisibility(View.VISIBLE);

                    if (movieList != null && movieList.size() > 0) {
                        showItemUi(videoDisImage1, videoDisName1, videoDisTv1, videoDisPlayNum1, movieList.get(0));
                    }

                    if (movieList != null && movieList.size() > 1) {
                        showItemUi(videoDisImage2, videoDisName2, videoDisTv2, videoDisPlayNum2, movieList.get(1));
                    }

                    if (movieList != null && movieList.size() > 2) {
                        showItemUi(videoDisImage3, videoDisName3, videoDisTv3, videoDisPlayNum3, movieList.get(2));
                    }
                }


            }


        }
    }


    private void showItemUi(ImageView imageView, TextView tv1, TextView tv2, TextView tv3, MovieInfo69Bean bean) {


        //显示图片
        Glide.with(context)
                .load(bean.getImg_url())
                .asBitmap()
                .into(imageView);


        //显示名字
        tv1.setText(bean.getTitle());

        //显示描述
        tv2.setText(bean.getDesc());

        //显示播放次数
        int num = 0;
        try {
            num = Integer.parseInt(bean.getPlay_nums());
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        String sType;
        if (num > 10000) {
            String fNum = df.format(num * 1.0f / 10000);
            sType = "播放数：" + fNum + "万次播放";
        } else {
            sType = "播放数：" + num + "次播放";
        }
        tv3.setText(sType);

    }


    /**
     * 点赞接口
     *
     * @param id 评论的ID
     */
    private void likeFun(String id, final CommentInfoBean bean) {

        String token = PreferenceUtils.getString(Constents.TOKEN);

        VideoDataRequest.videoLike(token, id, new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (context != null) {
                    ToastUtil.showShortToast("点赞失败");
                }
            }

            @Override
            public void onResponse(String response, int id) {


                try {
                    JSONObject jsonObject = new JSONObject(response);

                    if (!jsonObject.isNull("errno")) {

                        String code = jsonObject.getString("errno");
                        if ("0".equals(code)) {
                            if (context != null) {
                                ToastUtil.showShortToast("点赞成功");
                                bean.setHas_like("1");

                                String num = bean.getLikes();

                                try {
                                    int cout = Integer.parseInt(num);
                                    bean.setLikes(String.valueOf(cout + 1));
                                } catch (NumberFormatException e) {
                                    e.printStackTrace();
                                }

                                notifyDataSetChanged();
                            }

                        } else {
                            if (context != null) {
                                ToastUtil.showShortToast("点赞失败");
                            }
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

    }


}
